
#include "Wall3D.h"


/* Constructor
*
*/
Wall3D::Wall3D()
{
}

/* Constructor
*
*/
Wall3D::Wall3D(Ogre::Vector3 A, Ogre::Vector3 B, Ogre::Vector3 C, Ogre::Vector3 D) :
    m_A(A),
    m_B(B),
    m_C(C),
	m_ObjectType(WALL)
{
    m_ID = GameServices::ObjectManager()->GetWallCount();
    CalculateNormal();
}

/* Destructor
*
*/
Wall3D::~Wall3D()
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::~Wall3D() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::~Wall3D() ");
    }
}

/* Initialize
*
*/
void Wall3D::Initialize()
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::Initialize() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::Initialize() ");
    }
}

/* UpdateStarted(const Ogre::FrameEvent& gameTime)
*
*/
void Wall3D::UpdateStarted(const Ogre::FrameEvent& gameTime)
{
    if(GameServices::GameEngine()->GetLoggingLevel() == 5)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::UpdateStarted(const Ogre::FrameEvent& gameTime) | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() == 5)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::UpdateStarted(const Ogre::FrameEvent& gameTime) ");
    }
}

/* UpdateEnded(const Ogre::FrameEvent& gameTime)
*
*/
void Wall3D::UpdateEnded(const Ogre::FrameEvent& gameTime)
{
    if(GameServices::GameEngine()->GetLoggingLevel() == 5)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::UpdateEnded(const Ogre::FrameEvent& gameTime) | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() == 5)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::UpdateEnded(const Ogre::FrameEvent& gameTime) ");
    }
}

/* Cleanup()
*
*/
void Wall3D::Cleanup()
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::Cleanup() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::Cleanup() ");
    }
}


/* From()
* Return vector m_A
*/
Ogre::Vector3 Wall3D::From()
{
    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::From() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::From() ");
    }
    return m_A;
}

/* SetFrom(Ogre::Vector3 nA)
* Set vector m_A = nA
*/
void Wall3D::SetFrom(Ogre::Vector3 nA)
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::SetFrom(Ogre::Vector3 nA) | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    m_A = nA;
	CalculateNormal();

    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::SetFrom(Ogre::Vector3 nA) ");
    }
}

/* To()
* Return vector m_B
*/
Ogre::Vector3 Wall3D::To()
{
    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::To() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::To() ");
    }
    return m_B;
}

/* SetTo(Ogre::Vector3 nB)
* Set m_B = nB
*/
void Wall3D::SetTo(Ogre::Vector3 nB)
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::SetTo(Ogre::Vector3 nB) | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    m_B = nB;
	CalculateNormal();

    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::SetTo(Ogre::Vector3 nB) ");
    }
}

/* Height()
* Return vector m_C
*/
Ogre::Vector3 Wall3D::Height()
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::Height() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::Height() ");
    }
    return m_C;
}

/* SetHeight(Ogre::Vector3 nC)
* Set m_C = nC
*/
void Wall3D::SetHeight(Ogre::Vector3 nC)
{
    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::SetHeight(Ogre::Vector3 nC) | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    m_C = nC;
	CalculateNormal();

    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::SetHeight(Ogre::Vector3 nC) ");
    }
}

/* Width()
* Return vector m_D
*/
Ogre::Vector3 Wall3D::Width()
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::Width() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::Width() ");
    }
    return m_D;
}

/* SetWidth(Ogre::Vector3 nD)
* Set m_D = nD
*/
void Wall3D::SetWidth(Ogre::Vector3 nD)
{
    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::SetWidth(Ogre::Vector3 nD) | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    m_D = nD;

    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::SetWidth(Ogre::Vector3 nD) ");
    }
}

/* Normal()
* Return the pre-calculated normal vector
*/
Ogre::Vector3 Wall3D::Normal()
{
    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::Normal() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::Normal() ");
    }

    return m_N;
}

/* SetNormal(Ogre::Vector3 nN)
* Set m_N = nN
*/
void Wall3D::SetNormal(Ogre::Vector3 nN)
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::SetNormal(Ogre::Vector3 nN) | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    m_N = nN;

    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::SetNormal(Ogre::Vector3 nN) ");
    }
}

/* GetCenter()
* Calculate and return the center of the wall
* laying on the line AB
*/
Ogre::Vector3 Wall3D::GetCenter()
{
    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::GetCenter() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::GetCenter() ");
    }
    return ((m_A + m_B)/2);
}

/* CalculateNormal()
* 
*/
void Wall3D::CalculateNormal()
{
    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::CalculateNormal() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

	//Normalize the vector representing the length of the wall
	Ogre::Vector3 temp  = (m_B - m_A).normalisedCopy();

	//Swap the x and y components of the length to get the normal
	m_N.x = -temp.y;
	m_N.y = temp.x;
	m_N.z = 0;

    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::CalculateNormal() ");
    }
}

/* GetID()
* Return m_ID
*/
int Wall3D::GetID()
{
    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::GetID() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() >= 4)  //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::GetID() ");
    }
    return m_ID;
}

/* SetID(int id)
* Set m_ID = id
*/
void Wall3D::SetID(int id)
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::SetID(int id) | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    m_ID = id;

    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::SetID(int id) ");
    }
}

/* GetName()
* Return m_Name;
*/
Ogre::String Wall3D::GetName()
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::GetName() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::GetName() ");
    }
    return m_Name;
}

/* SetName(Ogre::String nName)
* Set m_Name = nName;
*/
void Wall3D::SetName(Ogre::String nName)
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::SetName(Ogre::String nName) | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

    m_Name = nName;

    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::SetName(Ogre::String nName) ");
    }
}

/* GetObjectType()
* Returns m_ObjectType
*/
ObjectType Wall3D::GetObjectType()
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::GetObjectType() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }


    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::GetObjectType() ");
    }
	return m_ObjectType;
}

void Wall3D::SetObjectType(ObjectType nType)
{
    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER Wall3D::GetObjectType() | ID #" +
                    Ogre::StringConverter::toString(m_ID));
    }

	m_ObjectType = nType;

    if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
    {
            Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT Wall3D::GetObjectType() ");
    }
}
